
#include <iostream>
#include "contact.pb.h"
#include <string>

//编译代码： g++ main.cc contacts.pb.cc -o TestProtoBuf -std=c++11 -lprotobuf

int main()
{
    std::string people_str;
    {
        // 将People信息进行序列化
        contacts::PeopleInfo people;
        people.set_name("张三");
        people.set_age(18);
        if (!people.SerializeToString(&people_str))
        {
            std::cerr << "序列化联系人失败!" << std::endl;
            return -1;
        }
        std::cout << "序列化成功，结果：" << people_str << std::endl;
    }

    {
        contacts::PeopleInfo people;
        if (!people.ParseFromString(people_str))
        {
            std::cerr << "反序列化联系人失败!" << std::endl;
            return -1;
        }
        std::cout << "反序列化联系人成功！" << std::endl
                  << "姓名： " << people.name() << std::endl
                  << "年龄： " << people.age() << std::endl;
    }
    return 0;
}




